package com.jiang.blackjack.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import com.jiang.blackjack.R
import com.jiang.blackjack.card.Card
import com.jiang.blackjack.card.CardIconHolder

/**
 *
 * @ProjectName:    BlackJack
 * @ClassName:      BJAdapter
 * @Description:     java类作用描述
 * @Author:         江
 * @CreateDate:     2020/3/1 14:43
 */
class BJAdapter(context: Context, list: ArrayList<Card>, private val showAll: Boolean) :
    ArrayAdapter<Card>(context, R.layout.item_lv_bj, list) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val v: View
        val holder: ViewHolder
        if (convertView == null) {
            v = LayoutInflater.from(context).inflate(R.layout.item_lv_bj, parent, false)
            holder = ViewHolder(v)
            v.tag = holder
        } else {
            v = convertView
            holder = v.tag as ViewHolder
        }
        val item = getItem(position)

        if (position > 0 && !showAll) {
            holder.iv_icon.setImageResource(CardIconHolder.DEFAULT)
        } else {
            item?.resIcon?.let { holder.iv_icon.setImageResource(it) }
        }

        return v
    }

    class ViewHolder(v: View) {
        val iv_icon: ImageView = v.findViewById(R.id.iv_card_icon)
    }
}